//
//  XGAdvertisementViewController.m
//  BSBuDeJie
//
//  Created by monkey on 2018/12/11.
//  Copyright © 2018 itcast. All rights reserved.
//

#import "XGAdvertisementViewController.h"
#import "XGTabBarController.h"

#import "XGAdvertisementManager.h"

#import "XGAdvertisementModel.h"


@interface XGAdvertisementViewController ()

/// 背景图片
@property (nonatomic,strong) UIImageView *backgroundImageView;
/// 跳过按钮
@property (nonatomic,strong) UIButton *skipButton;

/// 广告模型数据
@property (nonatomic,strong) XGAdvertisementModel *advertisementModel;

/// 定时器
@property (nonatomic,weak) NSTimer *timer;

@end

@implementation XGAdvertisementViewController

#pragma mark - 控制器生命周期方法

- (void)loadView
{
    self.view = self.backgroundImageView;
    [self.view addSubview:self.skipButton];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 展示广告
    [self showAdvertisement];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    // 销毁定时器
    [self.timer invalidate];
    self.timer = nil;
}

#pragma mark - 系统方法

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    // 子控件布局
    self.skipButton.frame = CGRectMake(self.view.width - self.skipButton.width - 15, 30, self.skipButton.width,  self.skipButton.height);
}

// 隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

#pragma mark - 事件监听

- (void)skipAction
{
     XGTabBarController *tabBarController = [[XGTabBarController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarController;
    tabBarController.view.alpha = 0;
    [UIView animateWithDuration:1 animations:^{
        tabBarController.view.alpha = 1;
    }];
}

- (void)tapBackgroundImageView
{
    NSURL *webUrl = [[XGAdvertisementManager sharedManager] webUrl];
    if (webUrl == nil) {
        return;
    }
    
    // 切换根控制器
    XGTabBarController *tabBarController = [[XGTabBarController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarController;
        // 发送点击图片通知
    [[NSNotificationCenter defaultCenter] postNotificationName:kTapAdvertisementNotificationName object:nil userInfo:@{kAdvertisementWebUrlKey : webUrl}];
}

#pragma mark - 其他方法

- (void)showAdvertisement
{
    UIImage *image = [[XGAdvertisementManager sharedManager] advertisementImage];
    if (image) {
        [self.backgroundImageView setImage:image];
    }
    
    [self countDown];
}

// 倒计时
- (void)countDown
{
    // 5秒倒计时
    static NSUInteger second = 3;
    if (second == -1) {
        [self skipAction];
        return;
    }
    
    [self.skipButton setTitle:[NSString stringWithFormat:@"跳过 %zd",second] forState:UIControlStateNormal];
    second--;
}

#pragma mark - 懒加载

- (UIImageView *)backgroundImageView
{
    if (_backgroundImageView == nil) {
        _backgroundImageView = [[UIImageView alloc] init];
        _backgroundImageView.userInteractionEnabled = YES;
        [_backgroundImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackgroundImageView)]];;
    }
    
    return _backgroundImageView;
}

- (UIButton *)skipButton
{
    if (_skipButton == nil) {
        _skipButton = [UIButton buttonWithTitle:nil fontSize:15 titleColor:[UIColor whiteColor] target:self action:@selector(skipAction)];
        _skipButton.size = CGSizeMake(60, 30);
        _skipButton.backgroundColor = [UIColor blackColor];
    }
    
    return _skipButton;
}
@end
